package com.xbongbong.crm.service.permission;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.abstracts.AbstractBusinessPermissionService;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.permission.pojo.dto.VerifyViewPermissionDTO;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.subform.SubFormTransferEnum;
import com.xbongbong.sys.model.UserModel;
import com.xbongbong.sys.util.UserPermissionUtil;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author: yingjf
 * @date: 2022/9/15 16:12
 * @description:
 */
@Service
public class CompetitorPermissionServiceImpl extends AbstractBusinessPermissionService<PaasFormDataEntityExt, PaasFormEntityExt> {
    @Resource
    private UserModel userModel;
    @Resource
    private EsHelper esHelper;

    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.COMPETITOR.getCode());
    }

    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        VerifyViewPermissionDTO verifyViewPermissionDTO = new VerifyViewPermissionDTO(loginUser, paasFormDataEntityExt);
        //校验团队权限&共享权限
        boolean hasDataPermission = hasPermission(formEntity, paasFormDataEntityExt, loginUser, subBusinessType)
                || sharePermissionHelp.verifySharePermission4QueryWithRuleEngine(verifyViewPermissionDTO);
        if (!hasDataPermission) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }
    }

    @Override
    public Boolean checkPermissionByTeam(String corpid, Integer businessType, BoolQueryBuilder boolQueryBuilder) throws XbbException {
        return checkPermissionByTeam(boolQueryBuilder, IndexTypeEnum.IDX_SAAS_COMPETITOR_TEAM);
    }

    @Override
    public boolean hasPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        if (UserPermissionUtil.isMaxDataPermission(loginUser, formEntity.getSaasMark(), formEntity.getBusinessType(), formEntity.getDistributorMark(), formEntity.getMenuId(), false)){
            return true;
        }
        String corpid = paasFormDataEntityExt.getCorpid();
        Long dataId = paasFormDataEntityExt.getId();
        List<String> userIdIn = getUserIdIn(loginUser);
        String department = loginUser.getDepartment();
        List<Long> deptIds = JSONArray.parseArray(department, Long.class);
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery(SubFormTransferEnum.getEsAttrByIndexTypeAndDbAttr(IndexTypeEnum.IDX_SAAS_COMPETITOR_TEAM, FieldTypeEnum.DATAID.getAlias()), dataId));
        boolQueryBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
        BoolQueryBuilder queryBuilder = boolQuery();
        boolQueryBuilder.should(queryBuilder.filter(termsQuery(SubFormTransferEnum.getEsAttrByIndexTypeAndDbAttr(IndexTypeEnum.IDX_SAAS_COMPETITOR_TEAM, ParameterConstant.USER_ID), userIdIn)));
        //部门
        BoolQueryBuilder queryBuilderTwo = boolQuery();
        boolQueryBuilder.should(queryBuilderTwo.filter(termsQuery(SubFormTransferEnum.getEsAttrByIndexTypeAndDbAttr(IndexTypeEnum.IDX_SAAS_COMPETITOR_TEAM, ParameterConstant.USER_ID), deptIds)));
        return checkPermissionByTeam(paasFormDataEntityExt.getCorpid(), subBusinessType, boolQueryBuilder);
    }
}
